package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.xmy.cultivate.entity.DealInfo;
import com.xmy.cultivate.entity.RepairCourse;
import com.xmy.cultivate.entity.TrackNum;
import com.xmy.cultivate.entity.Transcript;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.IDealInfoService;
import com.xmy.cultivate.service.ITrackDetailService;
import com.xmy.cultivate.service.ITrackService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author min
 * @since 2022-12-27
 */
@RestController
@RequestMapping("/api/trackDetail")
public class TrackDetailController extends BaseController{

    @Autowired
    ITrackDetailService iTrackDetailService;

    @Autowired
    TrackDetailMapper trackDetailMapper;

    @Autowired
    RepairCourseMapper repairCourseMapper;

    @Autowired
    IDealInfoService iDealInfoService;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    OrganizationMapper organizationMapper;



    @GetMapping("trackNumList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trackType" ,value = "类型" ,required = true,dataType = "String"),
            @ApiImplicitParam(name = "beginDate" ,value = "开始时间" ,required = true,dataType = "String"),
            @ApiImplicitParam(name = "endDate" ,value = "结束时间" ,required = true,dataType = "String"),
    })
    @ApiOperation("列表显示续报数目")
    public Result trackNumList(@RequestParam("trackType") String trackType,@RequestParam("beginDate")String beginDate,@RequestParam("endDate")String endDate){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(trackType.equals("")){
            queryWrapper.eq("classify","3");
        }else if(trackType.equals("1")){
            queryWrapper.eq("track_id","1671480213124182018");
            queryWrapper.eq("classify","3");
        }else if(trackType.equals("2")){
            queryWrapper.eq("track_id","1671480242631110657");
            queryWrapper.eq("classify","3");
        }
        queryWrapper.eq("type",1);
        queryWrapper.eq("deleted",0);
        List<Long> teacherIdList = trackDetailMapper.getSourceIdList(queryWrapper);
        List<TrackNum> TrackNumList = new ArrayList<>();
        for(Long teacherId : teacherIdList){
            TrackNum trackNum = trackDetailMapper.getTrackNum(beginDate,endDate, String.valueOf(teacherId));
            if(!Objects.isNull(trackNum)){
                trackNum.setTeacherName(repairCourseMapper.getTeacherName(trackNum.getTeacherId()));
                TrackNumList.add(trackNum);
            }else{
                trackNum = new TrackNum();
                trackNum.setTeacherName(repairCourseMapper.getTeacherName(teacherId.toString()));
                trackNum.setTeacherId(teacherId.toString());
                trackNum.setNowNumber("0");
                trackNum.setAllNumber("0");
                TrackNumList.add(trackNum);
            }
        }
        return new Result(Code.OK,TrackNumList,"好梦~");
    }
    @GetMapping("trackDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId" ,value = "老师id" ,required = true,dataType = "String"),
            @ApiImplicitParam(name = "beginDate" ,value = "开始时间" ,required = true,dataType = "String"),
            @ApiImplicitParam(name = "endDate" ,value = "结束时间" ,required = true,dataType = "String"),
    })
    @ApiOperation("列表显示续报细节")
    public Result trackDetail(@RequestParam Map reMap){
        String teacherId = (String) reMap.get("teacherId");
        String beginDate = (String) reMap.get("beginDate");
        String endDate = (String) reMap.get("endDate");
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<DealInfo> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);
        queryWrapper.between("add_date",beginDate,endDate);
        iDealInfoService.page(iPage,queryWrapper);
        for(DealInfo dealInfo : iPage.getRecords()){
            dealInfo.setSchoolName(organizationMapper.getSchoolName(dealInfo.getSchoolId()));
            dealInfo.setStudentName(studentMapper.selectById(dealInfo.getStudentId()).getName());
            dealInfo.setYearClassName(CommonUtil.getYearClassNameForId(dealInfo.getYearClassId()));
        }
        queryWrapper.eq("deleted",0);
        BigDecimal allCount = iDealInfoService.getAllCount(queryWrapper);
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("allCount",allCount);
        return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages(),objectMap),"好梦~");
    }
}
